<template>
  <BaseMainSlot ref="BaseMainSlot">
    <template v-slot:searchbox>
      <div class="seach-box">
        <div class="select-box-content">
          <div class="select-box-content-name">故障类型：</div>
          <el-select
            clearable
            v-model="hitType"
            placeholder="请选择"
            @change="selectHitchTypeCode(hitType)"
          >
            <el-option
              v-for="item in hitchTypeArr"
              :key="item.hitchTypeCode"
              :label="item.hitchTypeName"
              :value="item.hitchTypeCode"
            ></el-option>
          </el-select>
        </div>
        <div class="select-box-content">
          <div class="select-box-content-name">上报日期：</div>
          <el-date-picker
            v-model="applyDate"
            type="date"
            placeholder="选择日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd HH:mm:ss"
            clearable
            @change="queryPageList()"
          />
        </div>
        <div class="operationBtn">
          <div class="screenBtn">
            <div class="btn" @click="changeSelect()">
              <i
                :class="activeName === '' ? 'el-icon-d-arrow-right' : 'el-icon-d-arrow-left'"
                class="arrow-down"
              ></i>
            </div>
          </div>
        </div>
        <el-collapse v-model="activeName" accordion @change="handleChange">
          <el-collapse-item name="1">
            <div v-if="equipmentArr.length < 15" class="select-box-content">
              <div class="select-box-content-name">设备名称：</div>
              <div
                class="select-box-content-all"
                @click="cancelSelect('equipmentCode')"
              >全部</div>
              <div
                v-for="(item, index) in equipmentArr"
                :key="index"
                class="select-box-content-list"
                :style="{
                  color:
                    equipmentCode === item.equipmentCode
                      ? '#54DEE2'
                      : '#C1CFD2',
                  fontWeight:
                    equipmentCode === item.equipmentCode ? '600' : '400'
                }"
                @click="selectEquipmentCode(item.equipmentCode)"
              >{{ item.equipmentName }}</div>
            </div>
            <div v-else class="select-box-content">
              <div class="select-box-content-name">设备名称：</div>
              <el-select
                v-model="equipmentCode"
                filterable
                clearable
                placeholder="请选择设备名称"
                @change="queryPageList()"
              >
                <el-option
                  v-for="item of equipmentArr"
                  :key="item.equipmentCode"
                  :label="item.equipmentName"
                  :value="item.equipmentCode"
                />
              </el-select>
            </div>
          </el-collapse-item>
        </el-collapse>
      </div>
    </template>
    <template v-slot:typenodeslot>
      <CheckButton
        :topitem-select-list="topitemSelectList"
        @topitemtype="topitemtype"
      />
    </template>
    <template v-slot:content>
      <e-table
        rowKey="hitchApplyAuditCode"
        :table-data="tableData"
        :table-columns="tableLabel"
        :showSelectColumn="false"
        :page="currentPage"
        :size="pageSize"
        :total="totalSize"
        :loading="loading"
        :table-option="topitemtypeId === '待审核' ? tableOption : {}"
        :table-column-sum="topitemtypeId"
        @sizeChange="changePageSize"
        @pageChange="changeCurrentPage"
        @tableUpdatePlus="mixTableUpdatePlus"
      >
        <template slot="applyDate" slot-scope="scoped">
          <span
            v-if="scoped.row.hitchApplyAuditCode"
          >{{ dateToFormatTwo(scoped.row.applyDate) }}</span>
        </template>
        <template slot="keyword" slot-scope="scoped">
          <span
            v-if="scoped.row.scrapTaskId"
            class="table-btns-box"
            @click="queryKey(scoped.row)"
          >
            <i class="el-icon-view"></i>
            <span>查看关键字</span>
          </span>
        </template>
        <template slot="pictures" slot-scope="scoped">
          <span
            v-if="scoped.row.hitchApplyAuditCode"
            class="table-btns-box"
            @click="queryPicture(scoped.row)"
          >
            <i class="el-icon-view"></i>
            <span>查看图片</span>
          </span>
        </template>
        <template slot="videos" slot-scope="scoped">
          <span
            class="table-btns-box"
            v-if="scoped.row.hitchApplyAuditCode"
            @click="queryVideo(scoped.row)"
          >
            <i class="el-icon-view"></i>
            <span>查看视频</span>
          </span>
        </template>

        <template slot="hitchApplyAuditPersonMapper" slot-scope="scoped">
          <span
            class="table-btns-box"
            v-if="scoped.row.hitchApplyAuditCode&&scoped.row.approvalType"
            @click="queryhitchApplyAudit(scoped.row)"
          >
            <i class="el-icon-view"></i>
            <span>查看审批进度</span>
          </span>
        </template>
        <template
          slot="operate"
          v-if="scoped.row.hitchApplyAuditCode&&topitemtypeId === '待审核'"
          slot-scope="scoped"
        >
          <span
            v-if="judgePermission(183, 'approve')&& hitchApplyAuditPerson(scoped.row)"
            class="table-btns-box"
            @click="scrapHandleDialog(scoped.row)"
          >
            <i class="el-icon-circle-check"></i>
            <span>审核通过</span>
          </span>
          <span
            class="table-btns-box"
            v-if="judgePermission(183, 'approve') && topitemtypeId==='待审核' && hitchApplyAuditPerson(scoped.row)"
            @click="nocheck(scoped.row)"
          >
            <i class="el-icon-circle-check"></i>
            <span>审核不通过</span>
          </span>

          <span
            v-if="!hitchApplyAuditPerson(scoped.row)&&scoped.row.approvalType === '依次审批' || !hitchApplyAuditPerson(scoped.row)&&scoped.row.approvalType === '会签'"
          >请等待其他审核人审核</span>
          <span
            class="table-btns-box"
            v-if="judgePermission(183, 'revoke')"
            @click="revoceHandleDialog(scoped.row)"
          >
            <i class="el-icon-edit"></i>
            <span>撤销</span>
          </span>
        </template>

        <!-- <template
          slot="revoke"
          v-if="scoped.row.hitchApplyAuditCode&&topitemtypeId === '待审核'"
          slot-scope="scoped"
        >

        </template>-->
      </e-table>

      <NoCheckDialog
        ref="NoCheckDialog"
        :edit-object="editObject"
        :show.sync="showAddEditDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
      <HitchApply
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showhitchApplyDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
      <Video
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showVideoDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
      <Picture
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showPictureDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
      <Key
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showKeyDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
      <ShowHandleDialog
        :titleText="titleText"
        :message="dialogMessage"
        :showHandleDialog.sync="showHandleDialog"
        @leaveDialog="leaveDialog"
        @save="approveAudit"
      />
    </template>
  </BaseMainSlot>
</template>

<script>
import CheckButton from "@/components/equipTypeSelectButton/selcetButton.vue";
import ActionBar from "@/components/ActionBar";
import outsourcedRepairAuditApi from "@/api/equip-repair-manage/outsourced-repair-audit";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new.vue";
import HandleDialog from "../components/outsourced-repair-handle-dialog.vue";
import { mymixin } from "@/utils/mixin";
// import MessageDialog from "@/components/detailDialog/message-dialog.vue";
import NoCheckDialog from "./components/nocheck-dialog.vue";
import equipScrapRecordApi from "@/api/equip-manage/equip-scrap-record";
import repairTypeApi from "@/api/equip-repair-manage/repair-type";
import Video from "@/components/detailDialog/video-dialog.vue";
import Picture from "@/components/detailDialog/picture-dialog.vue";
import HitchApply from "@/components/detailDialog/hitchApply-dialog.vue";
import Key from "@/components/detailDialog/key-dialog.vue";
import ShowHandleDialog from "./components/HandleDialog.vue";
import scrapAuditApi from "@/api/equip-repair-manage/destory-manage/index";
import userApi from "@/api/system-settings/user-manage";
import { mapGetters } from "vuex";
import RepealApi from "@/api/equip-repair-manage/failure-to-report/index";
export default {
  name: "DestoryAudit",
  components: {
    BaseMainSlot,
    HandleDialog,
    NoCheckDialog,
    ActionBar,
    CheckButton,
    HitchApply,
    Video,
    Picture,
    Key,
    ShowHandleDialog,
  },
  mixins: [mymixin],
  data() {
    return {
      // 过审相关
      dialogMessage: "",
      showHandleDialog: false,

      hitType: "",
      // 搜索条件:故障名称：
      hitchTypeCode: "",
      hitchTypeArr: [],
      //  搜索条件:设备名称：
      equipmentCode: "",
      equipmentArr: [],
      //  搜索条件:上报日期
      applyDate: "",
      // 查看视频
      showVideoDialog: false,
      // 查看图片
      showPictureDialog: false,
      // 查看关键字
      showKeyDialog: false,
      showhitchApplyDialog: false,
      // 待审核
      topitemtypeId: "待审核",
      // 审核不通过
      nocheckActive: false,
      topitemSelectList: [
        { id: "待审核", name: "待审核" },
        { id: "审核通过", name: "审核通过" },
        { id: "审核未通过", name: "审核未通过" },
      ],
      userList: [],
      // 表单展示项
      tableLabel: [
        {
          label: "设备名称",
          param: "equipmentName",
          show: true,
          type: "Select",
          selectValue: "equipmentCode", // Select 实际查询参数
          typeObj: {
            // 动态数据和静态数据
            list: [],
            listName: ["equipmentName", "equipmentCode"], // 动态数据和静态数据数组字段名
          },
        },
        {
          label: "故障类型名称",
          param: "hitchTypeName", // 表格显示参数
          selectValue: "hitchTypeCode", // Select 实际查询参数
          show: true,
          type: "Select", // input Select Checkbox date
          typeObj: {
            // 动态数据和静态数据
            list: [],
            listName: ["hitchTypeName", "hitchTypeCode"], // 动态数据和静态数据数组字段名
          },
        },
        {
          label: "上报人",
          param: "proposer",
          show: true,
          selectValue: "userId", // Select 实际查询参数
          type: "Select", // input Select Checkbox date
          typeObj: {
            // 动态数据和静态数据
            list: [],
            listName: ["username", "userId"], // 动态数据和静态数据数组字段名
          },
        },
        {
          label: "上报日期",
          param: "applyDate",
          slotName: "applyDate",
          type: "date",
          show: true,
        },
        {
          label: "故障源",
          param: "discoverNodes",
          show: true,
        },
        {
          label: "故障关键字",
          param: "hitchKeywordNames",
          show: true,
          slotName: "keyword",
          notSearch: true,
        },

        {
          label: "故障图片",
          param: "pictures",
          show: true,
          slotName: "pictures",
          notSearch: true,
        },
        {
          label: "故障视频",
          param: "videos",
          show: true,
          slotName: "videos",
          notSearch: true,
        },
        {
          label: "审批类型",
          param: "approvalType",
          show: true,
          notSearch: true,
        },
        {
          label: "审批详情",
          param: "hitchApplyAuditPersonMapperList",
          slotName: "hitchApplyAuditPersonMapper",
          show: true,
          notSearch: true,
        },
      ],
      // 表单事件
      tableOption: {
        label: "操作",
        width: 240,
        options: [
          {
            label: "操作",
            type: "text",
            icon: "el-icon-circle-check",
            methods: "operate",
            slotName: "operate",
          },
          // {
          //   label: "过审",
          //   type: "text",
          //   icon: "el-icon-circle-check",
          //   methods: "approve",
          //   slotName: "approve",
          // },
          // {
          //   label: "撤销",
          //   type: "text",
          //   icon: "el-icon-circle-check",
          //   methods: "revoke",
          //   slotName: "revoke",
          // },
        ],
      },
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission",
    }),
  },
  created() {
    this.queryEquipmenList();
    this.queryhitchTypeList();
    this.queryUserList();

    this.queryPageList();
  },
  mounted() {},
  methods: {
    queryhitchApplyAudit(row) {
      this.titleText = "审核详情";
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showhitchApplyDialog = true;
    },
    hitchApplyAuditPerson(row) {
      if (row.result !== "待审核") return false;
      if (row.approvalType === "依次审批") {
        if (row.currentAuditUser === sessionStorage.personNo) {
          return true;
        } else {
          return false;
        }
      } else {
        let index = row.hitchApplyAuditPersonMapperList.findIndex(item => item.personNo === sessionStorage.personNo);
        if (index !== -1) {
          if (row.hitchApplyAuditPersonMapperList[index]['pass'] === true) {
            return false;
          } else {
            return true;
          }
        } else {
          return false;
        }
      }
    },
    // 审核不通过
    nocheck(row) {
      this.editObject = row;
      this.checkActive = false;
      this.nocheckActive = false;
      this.showAddEditDialog = true;
      this.showCheckDialog = false;
    },
    // 审核相关
    revoceHandleDialog(row) {
      this.showHandleDialog = true;
      this.titleText = "撤销申请";
      this.dialogMessage = "确定撤销该报废申请？";
      this.editObject = row;
    },
    scrapHandleDialog(row) {
      this.showHandleDialog = true;
      this.titleText = "报废审核";
      this.dialogMessage = "确定通过该报废申请？";
      this.editObject = row;
    },
    leaveDialog() {
      this.showHandleDialog = false;
    },
    approveAudit(val) {
      // 审核操作
      if (val === "报废审核") {
        var params = {
          result: "审核通过",
          hitchApplyAuditCode: this.editObject.hitchApplyAuditCode,
          personNo: sessionStorage.personNo,
          pass: true,
        };
        scrapAuditApi.scrapAuditNew(params).then((res) => {
          if (res.code === 1) {
            this.queryPageList();
            this.showHandleDialog = false;
            this.$message.success(res.msg);
          } else {
            this.$message.error(res.msg);
          }
        });
      } else {
        RepealApi.revokeAudit(this.editObject.hitchApplyAuditCode).then(
          (res) => {
            if (res.code === 1) {
              this.queryPageList();
              this.showHandleDialog = false;
              this.$message.success(res.msg);
            } else {
              this.$message.error(res.msg);
            }
          }
        );
      }
    },

    // 清空选项
    cancelSelect(data) {
      if (data === "hitchTypeCode") {
        this.hitchTypeCode = "";
      }
      if (data === "equipmentCode") {
        this.equipmentCode = "";
      }
      this.queryPageList();
    },
    // 选择
    selectHitchTypeCode(id) {
      if (this.hitchTypeCode === id) {
        this.hitchTypeCode = "";
      } else {
        this.hitchTypeCode = id;
      }
      this.queryPageList();
    },
    // 选择
    selectEquipmentCode(id) {
      if (this.equipmentCode === id) {
        this.equipmentCode = "";
      } else {
        this.equipmentCode = id;
      }
      this.queryPageList();
    },
    // 条件查询未报废设备
    queryEquipmenList() {
      this.equipmentArr = [];
      let params = {};
      equipScrapRecordApi.queryEquipment(params).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          for (var i in data) {
            this.equipmentArr.push({
              equipmentName: data[i],
              equipmentCode: i,
            });
          }
          this.setTableColumnsPopover("equipmentName", this.equipmentArr);
        }
      });
    },
    // 条件查询故障类型
    queryhitchTypeList() {
      this.hitchTypeArr = [];
      repairTypeApi.findAll({}).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          this.hitchTypeArr = data;
          this.setTableColumnsPopover("hitchTypeName", this.hitchTypeArr);
        }
      });
    },
    // 查询可选择维修人
    queryUserList() {
      userApi.queryUserAll({}).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          this.userList = data;
          this.setTableColumnsPopover("proposer", this.userList);
        }
      });
    },
    //表头 下拉框输入赋值
    setTableColumnsPopover(key, data) {
      let item = null;
      let index = 0;
      this.tableLabel.forEach((el, i) => {
        if (el.param === key && el.typeObj) {
          item = el;
          index = i;
          item.typeObj.list = data;
        }
      });
      item && this.$set(this.tableLabel, index, item);
    },
    // 审核通过,审核未通过,作废维修
    topitemtype(e) {
      this.tableData = [];
      this.currentPage = 1;
      this.topitemtypeId = e;
      // setTimeout(() => {
      //   this.$refs.BaseMainSlot.calculateHeight();
      // }, 200);
      // 获取表格数据
      this.queryPageList();
    },
    queryPageList(e) {
      this.loading = false;
      // 获取数据
      let param = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          hitchTypeCode: this.hitchTypeCode || void 0,
          equipmentCode: this.equipmentCode || void 0,
          applyDate: this.applyDate || e?.applyDate,
          repairNature: "报废", // 维修方式
          equipmentName: e?.equipmentName,
          ...e,
        },
      };
      // if (this.topitemtypeId === "待审核") {
      param.entity.result = this.topitemtypeId;
      // param.entity.auditUser = sessionStorage.account;
      // param.entity.handUsername = sessionStorage.account;
      // } else {
      //   param.entity.result = "审核通过";
      // }
      outsourcedRepairAuditApi.queryPageList(param).then((res) => {
        this.loading = false;
        if (res.code === 1) {
          let data = res.data;
          this.totalSize = data.totalElements;
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    // 维修作废
    // save() {
    //   var params = {
    //     result: "审核通过",
    //     hitchApplyAuditCode: this.editObject.hitchApplyAuditCode
    //   };
    //   outsourcedRepairAuditApi.editApplyOut(params).then(res => {
    //     if (res.code === 1) {
    //       this.queryPageList();
    //       this.closeDialog();
    //       this.$message.success(res.msg);
    //     } else {
    //       this.$message.error(res.msg);
    //     }
    //   });
    // },
    // 查看关键字
    queryKey(row) {
      this.titleText = "关键字";
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showKeyDialog = true;
    },
    // 查看视频
    queryVideo(row) {
      this.titleText = "故障视频";
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showVideoDialog = true;
    },
    // 查看图片
    queryPicture(row) {
      this.titleText = "故障图片";
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showPictureDialog = true;
    },
  },
};
</script>

<style lang="scss" scoped>
/* 禁用全选 */
::v-deep .has-gutter {
  .el-table-column--selection {
    &:first-of-type {
      .cell {
        display: none;
      }
    }
  }
}
::v-deep .action-bar {
  justify-content: center;
  .btn-list {
    .btn-item {
      margin-left: unset;
    }
  }
}

// 复制
::v-deep .el-table {
  margin-top: 20px;
}
::v-deep .el-collapse-item__header {
  height: 0px;
}
::v-deep .content-manage .common-layout {
  margin-top: 30px;
}
::v-deep .butt-list-sty {
  top: -15px;
}
::v-deep .bf {
  border-color: #fff;
  color: #fff;
  background-color: rgb(244, 82, 82);
}
.same {
  border-color: #fff;
  color: #fff;
}
.cs {
  background-color: rgb(94, 200, 82);
}
.zh {
  background-color: rgb(49, 137, 237);
}
.xh {
  background-color: rgb(244, 82, 82);
}
</style>
